<template>
  <page-header-wrapper>
    <page-tab-list>
      <div class="table-page-search-box">
        <a-form layout="inline">
          <a-row :gutter="24">
            <a-col :md="8" :sm="24">
              <a-form-item label="姓名">
                <a-input allowClear placeholder="请输入姓名" v-model="queryParam.name" />
              </a-form-item>
            </a-col>
            <a-col :md="8" :sm="24">
              <a-form-item label="手机号">
                <a-input allowClear placeholder="请输入手机号" v-model="queryParam.mobile" />
              </a-form-item>
            </a-col>
            <a-col :md="8" :sm="24">
              <a-form-item label="">
                <a-button type="primary" @click="getList">查询</a-button>
              </a-form-item>
            </a-col>
          </a-row>
        </a-form>
      </div>
    </page-tab-list>

    <a-card :bordered="false">
      <div class="table-operator" style="display: flex; justify-content: space-between">
        <a-radio-group v-model="queryParam.status" @change="getList">
          <a-radio-button value="-1"> 全部 </a-radio-button>
          <a-radio-button value="0"> 未审核 </a-radio-button>
          <a-radio-button value="1"> 已通过 </a-radio-button>
          <a-radio-button value="2"> 已拒绝 </a-radio-button>
        </a-radio-group>
        <div></div>
      </div>
      <a-alert type="info" show-icon style="margin: 10px 0" v-if="selectedRowKeys.length">
        <span slot="message">
          已选择 <span style="color: red">{{ selectedRowKeys.length }}</span> 项
        </span>
      </a-alert>
      <a-table
        :rowKey="
          (r, i) => {
            return r.id
          }
        "
        :pagination="{
          total: this.totalCount,
          showSizeChanger: false,
          onChange: this.tablePageChange,
        }"
        :columns="columns"
        :rowSelection="{
          selectedRowKeys: this.selectedRowKeys,
          onChange: (e) => {
            this.selectedRowKeys = e
          },
        }"
        :data-source="list"
        :scroll="{ x: 'max-content' }"
      >
        <div slot="user" slot-scope="text, record">
          <div class="name">
            <div class="pic">
              <img :src="record.avatar" alt="" />
            </div>
            <div class="info">
              <span>{{ record.nickname }}</span>
            </div>
          </div>
        </div>
        <span slot="status" slot-scope="text">
          <div class="tagBox">
            <span class="tag" :style="text | listStatus"></span>
            <span>{{ getStatus(text) }} </span>
          </div>
        </span>

        <div slot="action" slot-scope="scope">
          <a-button type="link" @click="see(scope)" size="small">查看</a-button>
          <a-divider type="vertical" style="margin: 0 1px" />
          <a-dropdown :disabled="scope.status != '0'">
            <a-button type="link" size="small"> 更多 <a-icon type="down" /> </a-button>
            <a-menu slot="overlay">
              <a-menu-item>
                <a-popconfirm title="确认同意吗？" @confirm="handelStatus(1, scope)">
                  <a href="javascript:;">通过</a>
                </a-popconfirm>
              </a-menu-item>
              <a-menu-item>
                <a-popconfirm title="确认拒绝吗？" @confirm="handelStatus(2, scope)">
                  <a href="javascript:;">拒绝</a>
                </a-popconfirm>
              </a-menu-item>
              <a-menu-item>
                <a-popconfirm title="确认删除吗？" @confirm="handelDelete(scope)">
                  <a href="javascript:;">删除</a>
                </a-popconfirm>
              </a-menu-item>
            </a-menu>
          </a-dropdown>
        </div>
      </a-table>
    </a-card>
    <a-modal title="查看采购商信息" style="width: 300px" v-model="visible" size="small" :footer="null">
      <div v-if="current">
        <div class="info">
          <span class="info-title">用户:</span>
          <span class="info-content">
            <img :src="current.avatar" alt="" />
            {{ current.nickname }}
          </span>
        </div>
        <div class="info">
          <span class="info-title">手机号:</span>
          <span class="info-content">
            {{ current.mobile }}
          </span>
        </div>
        <div class="info" v-for="(item, index) of current.custom_data" :key="index">
          <span class="info-title">{{ item.name }}:</span>
          <span class="info-content" v-if="item.type != 'uploadImg'">
            {{ item.default }}
          </span>
          <span class="info-content" v-else>
            <img :src="item.default" style="width: 100px; height: auto; display: inline-block" alt="" />
          </span>
        </div>
      </div>
    </a-modal>
  </page-header-wrapper>
</template>

<script>
import { getPurchaseList, applyPurchase, deletePurchase } from '@/api/purchase/purchase'

const columns = [
  {
    title: 'ID',
    dataIndex: 'id',
    width: 80,
  },
  {
    title: '用户',
    scopedSlots: {
      customRender: 'user',
    },
    width: 280,
  },
  {
    title: '姓名',
    dataIndex: 'name',
    width: 280,
  },
  {
    title: '电话',
    dataIndex: 'mobile',
  },
  {
    title: '状态',
    dataIndex: 'status',
    scopedSlots: {
      customRender: 'status',
    },
  },
  {
    title: '操作',
    width: 220,
    scopedSlots: {
      customRender: 'action',
    },
  },
]

export default {
  components: {},

  computed: {
    getStatus() {
      return (status) => {
        let str = '未知'
        switch (parseInt(status)) {
          case 0:
            str = '未审核'
            break

          case 1:
            str = '已通过'
            break

          case 2:
            str = '已拒绝'
            break
        }

        return str
      }
    },
  },

  data() {
    return {
      // 搜索参数
      queryParam: {
        pageNo: 1,
        name: '',
        status: '-1',
        mobile: '',
      },

      selectedRowKeys: [],
      columns,
      list: [],
      btnLoading: false,
      visible: false,
      current: null,
    }
  },

  created() {
    this.getList()
  },

  filters: {
    listStatus(val) {
      if (val == 0) {
        return 'background-color:#faad14;'
      }

      if (val == 1) {
        return 'background-color:#65c817;'
      }

      if (val == 2) {
        return 'background-color:#ff0000;'
      }
    },
  },

  methods: {
    see(item) {
      this.current = item
      this.visible = true
    },
    async handelDelete(scope) {
      const hide = this.$message.loading('操作中...', 1000)
      const res = await deletePurchase({
        id: scope.id,
      })
      hide()
      if (res.code == 0) {
        this.$message.success('操作成功')
        this.getList()
      } else {
        this.$message.error(res.msg)
      }
    },
    async handelStatus(status, scope) {
      const hide = this.$message.loading('操作中...', 1000)
      const res = await applyPurchase({
        id: scope.id,
        status: status,
      })
      hide()
      if (res.code == 0) {
        this.$message.success('操作成功')
        this.getList()
      } else {
        this.$message.error(res.msg)
      }
    },
    // 分页请求
    tablePageChange(e) {
      this.queryParam.pageNo = e
      this.getList()
    },

    // 获取数据
    async getList() {
      const hide = this.$message.loading('加载中...', 1000)
      const res = await getPurchaseList(this.queryParam)
      if (res.code == 0) {
        this.list = res.data.list
        this.totalCount = res.data.totalCount
      }
      hide()
    },
  },
}
</script>
<style lang="less" scoped>
.searchBox {
  display: flex;
  align-items: center;
  flex-wrap: wrap;
  /deep/ .ant-input-affix-wrapper {
    width: 220px;
    margin-right: 20px;
  }
  .select {
    margin-top: 11px;
    display: flex;
    align-items: center;
    margin-right: 15px;
  }
  .ant-btn {
    margin-top: 11px;
  }
}
.tip {
  line-height: normal;
  margin-bottom: 0;
  font-size: 14px;
  color: rgba(0, 0, 0, 0.45);
}
.name {
  display: flex;
  align-items: center;
  .pic {
    width: 48px;
    min-width: 48px;
    height: 48px;
    border-radius: 4px;
    overflow: hidden;
    margin-right: 24px;
    img {
      width: 100%;
      height: 100%;
    }
  }
  .info {
    flex: auto;
    width: 0;
    display: flex;
    flex-direction: column;
    font-size: 14px;
    span {
      width: 100%;
      overflow: hidden;
      white-space: nowrap;
      text-overflow: ellipsis;
    }
    span:nth-child(2) {
      color: rgba(0, 0, 0, 0.45);
      margin-top: 6px;
    }
  }
}

.tagBox {
  display: flex;
  align-items: center;
}

.tag {
  display: inline-block;
  width: 6px;
  height: 6px;
  border-radius: 50%;
  margin-right: 4px;
}

::v-deep {
  .card_top_padding .ant-card-body .ant-tabs-bar {
    margin-bottom: 0;
  }
}
.info-title {
  width: 60px;
  display: inline-block;
  margin-right: 30px;
}
.info-content img {
  width: 40px;
  height: 40px;
  border-radius: 3px;
}
.info {
  padding-bottom: 20px;
  margin-bottom: 20px;
  border-bottom: 1px solid #f2f2f2;
}
.info:last-child {
  margin-bottom: 0;
  padding-bottom: 0;
  border-bottom: none;
}
</style>
